﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Web.Configuration;
using Domain;
using DataAcess;
using System.Net.Mail;
using System.Net;

namespace HotelBrisayArena
{
    public partial class RegistroReservacion : System.Web.UI.Page
    {
        //no se pueda reservar una fecha del pasado 
        Reservacion reservacion = new Reservacion();
        ImagenBusiness imgBusiness = new ImagenBusiness(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString()); 
        TipoHabitacionBusiness tipoHabitacionBusiness = new TipoHabitacionBusiness(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
        HabitacionBusiness habitacionBusiness = new HabitacionBusiness(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
        TipoHabitacion tipoHabitacion = new TipoHabitacion();
        Habitacion habitacion = new Habitacion();
        protected void Page_Load(object sender, EventArgs e)
        {
            //Imagen imagen2 = new Imagen();
            reservacion = (Reservacion)Session["reservacion"];
            habitacion = habitacionBusiness.ObtenerHabitacion(reservacion.IdHabitacion);
            tipoHabitacion = tipoHabitacionBusiness.ObtenerTipoHabitacion(habitacion.TipoHabitacion.IdTipoHab);
            imgBusiness.GetImagen(tipoHabitacion.IdImagen);
            Label1.Text = tipoHabitacion.Descripcion;
            Label2.Text = tipoHabitacion.Tarifa.ToString();
            Label3.Text = tipoHabitacion.Tipo;

            //imagen2 = imgBusiness.GetImagen(tipoHabitacion.IdImagen);
            //byte[] imageBytes2 = imagen2.Image;
            //string base64ImageString2 = ConvertBytesToBase64(imageBytes2);
            //Imagen1.Src = "data:image/jpg;base64," + base64ImageString2;

            byte[] imageBytes = imgBusiness.GetImagen(tipoHabitacion.IdImagen).Image;
            string base64ImageString = ConvertBytesToBase64(imageBytes);
            Imagen1.Src = "data:image/jpg;base64," + base64ImageString;
             
             
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ReservacionBusiness reservacionBusiness = new ReservacionBusiness(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
            try
            {
                reservacion.NombreCliente = TextBox7.Text;
                reservacion.ApellidosCliente = TextBox8.Text;
                reservacion.Email = TextBox9.Text;
                reservacion.TarjetaCredito = TextBox10.Text;
                reservacionBusiness.registrarReservacion(reservacion);
                
                Session["reservacion"] = reservacion;
                Response.Redirect("Exito.aspx");
            }catch(FormatException)
            {
               
            }
            MailMessage msg = new MailMessage();



            msg.To.Add(new MailAddress("randallcerdasjackson@gmail.com"));

            msg.From = new MailAddress("randallcerdasjackson@gmail.com");

            msg.Subject = "El asunto del mensaje(2.0)";

            msg.Body = "El contenido del mensaje";



            SmtpClient clienteSmtp = new SmtpClient("WIN02");

            clienteSmtp.Credentials = new NetworkCredential("randallcerdasjackson@gmail", "ramirezflores1191jose");

 

            try
            {

                clienteSmtp.Send(msg);

            }

            catch (Exception ex)
            {

                Console.Write(ex.Message);

                Console.ReadLine();

            }
        }
        
        public string ConvertBytesToBase64(byte[] imageBytes)
        {
            return Convert.ToBase64String(imageBytes);
        }
    }
}